package com.zlzkj.app.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.zlzkj.app.model.Energymac;

public class Excel {    
	public static HSSFFont font;
	public static HSSFCellStyle style;
	public static HSSFCellStyle titleStyle;

	public HSSFWorkbook doExcel(List list,int count) {
		// 创建Excel的工作书册 Workbook,对应到一个excel文档
	    HSSFWorkbook wb = new HSSFWorkbook();
	    init(wb);
	    // 创建Excel的工作sheet,对应到一个excel文档的tab
	    HSSFSheet sheet = wb.createSheet("sheet1");
	// 创建Excel的sheet的一行

	    //暂时写死
	    for(int k = 0 ;k<count;k++){
	    	HSSFRow firstRow = sheet.createRow(0);
	    	HSSFCell title1 = firstRow.createCell(0);
	    	title1.setCellValue("消耗能源名称");
	    	
	    	HSSFCell title2 = firstRow.createCell(1);
	    	title2.setCellValue("消耗速率");
	    	
	    	HSSFCell title3 = firstRow.createCell(2);
	    	title3.setCellValue("消耗额度");
	    	
	    	HSSFCell title4 = firstRow.createCell(3);
	    	title4.setCellValue("历史消耗速率");
	    	
	    	HSSFCell title5 = firstRow.createCell(4);
	    	title5.setCellValue("机器状态");
	    	
	    	HSSFCell title6 = firstRow.createCell(5);
	    	title6.setCellValue("机器组别");
	    	
		    HSSFRow row = sheet.createRow(k+1);		
		    HSSFCell energy = row.createCell(0);
		    energy.setCellStyle(style);
		    if(((Energymac) list.get(k)).getEnergy()==1)
		    	energy.setCellValue("自来水");
		    if(((Energymac) list.get(k)).getEnergy()==2)
		    	energy.setCellValue("电力");
		    
		    HSSFCell rate = row.createCell(1);
		    rate.setCellStyle(style);
		    rate.setCellValue(((Energymac) list.get(k)).getRate());
		    
		    HSSFCell consume = row.createCell(2);
		    consume.setCellStyle(style);
		    consume.setCellValue(((Energymac) list.get(k)).getConsume());
		    
		    HSSFCell hisrate = row.createCell(3);
		    hisrate.setCellStyle(style);
		    hisrate.setCellValue(((Energymac) list.get(k)).getHisrate());
		    
		    HSSFCell state = row.createCell(4);
		    state.setCellStyle(style);
		    if(((Energymac) list.get(k)).getState()==0)
		    	state.setCellValue("启用");
		    else{state.setCellValue("禁用");}
		    
		    HSSFCell groups = row.createCell(5);
		    groups.setCellStyle(style);
		    groups.setCellValue(((Energymac) list.get(k)).getGroups());
			/*}*/
	    }
	    try {	
	    	
	    	wb.write(new FileOutputStream(new File("F:\\energy\\add.xls")));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return wb;
	}
	
	 public static void init(HSSFWorkbook wb) {
		// 创建字体样式

	        font = wb.createFont();
	        font.setFontName("Verdana");
	        font.setBoldweight((short) 100);
	        font.setFontHeight((short) 100);
	        font.setColor(HSSFColor.BLACK.index);

	      //标题样式
	        HSSFFont titleFont = wb.createFont();
	        titleFont.setFontName("Verdana");
	        //titleFont.setBold(true);
	        titleFont.setFontHeight((short) 150);
	        titleFont.setFontHeight((short) 300);
	        titleFont.setColor(HSSFColor.BLACK.index);

	        titleStyle = wb.createCellStyle();
	        titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	        titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
	        titleStyle.setFont(titleFont);

	     // 创建单元格样式
	        style = wb.createCellStyle();
	        //style.setShrinkToFit(true);
	        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
	        //style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
	        //style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

	     // 设置边框
	        style.setBottomBorderColor(HSSFColor.BLACK.index);
	        style.setLeftBorderColor(HSSFColor.BLACK.index);
	        style.setRightBorderColor(HSSFColor.BLACK.index);
	        style.setTopBorderColor(HSSFColor.BLACK.index);
	        style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
	        style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
	        style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
	        style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);

	        //自动换行
	        style.setWrapText(true);

	        style.setFont(font);// 设置字体F
	    }    
}